package com.lg.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.product.entity.Sku;
import com.lg.product.entity.Stock;
import com.lg.product.service.ISkuService;
import com.lg.product.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * sku表,该表表示具体的商品实体,如黑色的 64g的iphone 8 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/sku")
public class SkuController {

    @Autowired
    public ISkuService iSkuService;
    @Autowired
    private IStockService iStockService;

    @RequestMapping("listSku")
    public IPage<Sku> listSku(Sku sku, Page page){
        IPage<Sku> list= iSkuService.listSku(sku,page);
        List<Sku> records = list.getRecords();
       /* records.forEach(re->{
            Long id = re.getId();
            List<Stock> list1 = iStockService.list();
                    list1.forEach(li->{
                        if(re.getId()==li.getSkuId()){
                            re.setStock(li.getStock());
                        }
                    });
        });*/
        return list;
    }

    @RequestMapping("/updateEnable")
    public boolean updateEnable(@RequestBody Sku sku){
        boolean b = iSkuService.updateById(sku);
        return b;
    }
    @RequestMapping("/update")
    public boolean update(@RequestBody Sku sku){

        Stock stock = new Stock();
        stock.setStock(sku.getStock());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sku_id",sku.getId());
        iStockService.update(stock,queryWrapper);
        return iSkuService.updateById(sku);
    }
}
